/*
 * module.c
 *
 *  Created on: 2009-5-8
 *      Author: thh
 */

#include "base.h"
#include <openssl/err.h>
#include <openssl/ssl.h>

PyMethodDef mod_meth[]={
		{NULL}
};

PyModuleDef mod_def = {
		PyModuleDef_HEAD_INIT,
		MODULE_NAME,
		"Python cryptic extension",
		-1,
		mod_meth,
		NULL,NULL,NULL,NULL
};

PyObject *PyExc_OpenSSLError = NULL;

void PyErr_OpenSSLError()
{
	unsigned long err =  ERR_get_error();
	char *txt = ERR_error_string(err,NULL);

	PyErr_SetString(PyExc_OpenSSLError,txt);
}

PyMODINIT_FUNC PyInit_pyCryptic(void)
{
	PyObject *module = PyModule_Create(&mod_def);

	if(PyType_Ready(&PyRSA_Type)<0)
		return NULL;
	if(PyType_Ready(&PyPem_Type)<0)
		return NULL;

	PyModule_AddObject(module,"RSA",(PyObject *)&PyRSA_Type);
	PyModule_AddObject(module,"Pem",(PyObject *)&PyPem_Type);

	ERR_load_crypto_strings();
	SSL_load_error_strings();

	PyExc_OpenSSLError = PyErr_NewException(MODULE_NAME".SSLError",PyExc_Exception,NULL);
	if(!PyExc_OpenSSLError){
	     PyErr_SetString(PyExc_ImportError, MODULE_NAME ": init failed");
	     Py_DECREF(module);
	     return NULL;
	}
	PyModule_AddObject(module,"SSLError",PyExc_OpenSSLError);
	return module;
}
